//
//  TodoList.swift
//  NaiCha
//
//  Created by 唐安坤 on 2024/9/19.
//

import SwiftUI

struct TodoList: View {
    
    @State var todos: [Todos] = [Todos.init(todoName: "aaaaaaaaa", priority: .high)]
    @State var goTodoView:Bool = false
    var body: some View {
        NavigationStack {
            VStack{
                if todos.count > 0 {
                    List {
                        ForEach($todos){model in
                            cellView(model: model)
                                .swipeActions(edge: .leading, allowsFullSwipe: false){
                                    HStack{
//                                        
//                                        Button("aa", image: ImageResource.init(name: "dog01", bundle: .main)) {
//                                            model.isMark.wrappedValue.toggle()
//                                        }
                                        Button.init(action: {
                                            model.isMark.wrappedValue.toggle()
                                        }, label: {
                                            HStack{
                                                Image.init(systemName: "star.fill").font(.system(size: 40))
                                            }
                                        }).tint(.yellow)
                                    }
                                }
                        }
                        
                        .onMove(perform: { indices, newOffset in
                            todos.move(fromOffsets: indices, toOffset: newOffset)
                        }).onDelete(perform: { indexSet in
                            todos.remove(atOffsets: indexSet)
                        })
                        .listRowSeparatorTint(.red)
                    }
                    
                    .listStyle(.plain)
                }else{
                    Image("empty", bundle: nil)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                }
            } .animation(.default, value: todos.count)
                .navigationTitle("待办事项")
                .toolbar(content: {
                    Menu {
                        Button(action: {
                            goTodoView = true
                        }, label: {
                            Label(
                                title: { Text("添加代办") },
                                icon: { Image(systemName: "42.circle") }
                            )
                        })
                        
                        Button(action: {
                            
                        }, label: {
                            Label(
                                title: { Text("编辑模式") },
                                icon: { Image(systemName: "42.circle") }
                            )
                        })
                        
                    } label: {
                        Image.init(systemName: "slider.horizontal.3").foregroundColor(.gray)
                    }.foregroundColor(.gray)
                    
                }).sheet(isPresented: $goTodoView, content: {
                    TodoView.init { todo in
                        todos.append(todo)
                    }
                })
                .toolbar(content: {
                    Button.init(action: {
                        print(todos)
                    }, label: {
                        Text("Button")
                    })
                })
            //            .navigationDestination(isPresented: $goTodoView) {
            //                TodoView.init { todo in
            //                    todos.append(todo)
            //                }
            //            }
            
        }
    }
}

#Preview {
    TodoList()
}

struct cellView: View {
    @Binding var model: Todos
    var body: some View {
        
        HStack{
            Toggle(isOn: $model.isFinish, label: {
                Text(model.todoName).strikethrough(model.isFinish, pattern: .solid, color: .red)
            }).toggleStyle(CustomToggleStyley())
            
            Spacer()
            
            if model.isMark{
                Image(systemName: "star.fill").foregroundStyle(.yellow)
            }
            
            Circle().frame(width: 10,height: 10).foregroundColor(getColor(priority: model.priority))
        }
    }
    
    func getColor(priority: PriorityEnum) ->Color{
        switch priority{
        case .high:
            return .red
        case .normal:
            return .green
        case .low:
            return .gray
        }
    }
}
